{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "[Sven Kreiss](https://www.svenkreiss.com/), 2020\n",
    "\n",
    "# Prediction\n",
    "\n",
    "Use OpenPifPaf from the command line to run multi-person pose estimation on images.\n",
    "For programmatic predictions, please refer to {doc}`predict_api`.\n",
    "Below is a short intro to running prediction on videos.\n",
    "\n",
    "Run `openpifpaf.predict` on an image:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "!python -m openpifpaf.predict coco/000000081988.jpg --image-output --json-output --debug"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "This command produced two outputs: an image and a json file.\n",
    "You can provide file or folder arguments to the `--image-output` and `--json-output` flags.\n",
    "Here, we used the default which created these two files:\n",
    "\n",
    "```sh\n",
    "coco/000000081988.jpg.predictions.png\n",
    "coco/000000081988.jpg.predictions.json\n",
    "```\n",
    "\n",
    "Here is the image:\n",
    "\n",
    "![predictions](coco/000000081988.jpg.predictions.png)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "And below is the json output. The json data is a list where each entry in the list corresponds to one pose annotation. In this case, there are five entries corresponding to the five people in the image. Each annotation contains information on `\"keypoints\"`, `\"bbox\"`, `\"score\"` and `\"category_id\"`.\n",
    "\n",
    "All coordinates are in pixel coordinates. The `\"keypoints\"` entry is in COCO format with triples of `(x, y, c)` (`c` for confidence) for every joint as listed under {ref}`coco-person-keypoints`. The pixel coordinates have sub-pixel accuracy, i.e. 10.5 means the joint is between pixel 10 and 11.\n",
    "In rare cases, joints can be localized outside the field of view and then the pixel coordinates can be negative. When `c` is zero, the joint was not detected.\n",
    "\n",
    "The `\"bbox\"` (bounding box) format is `(x, y, w, h)`: the $(x, y)$ coordinate of the top-left corner followed by width and height.\n",
    "\n",
    "The `\"score\"` is a number between zero and one."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "!python -m json.tool coco/000000081988.jpg.predictions.json"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Optional Arguments\n",
    "\n",
    "* `--show`: show interactive matplotlib output\n",
    "* `--debug-images`: enable debug messages and debug plots\n",
    "\n",
    "Full list of arguments is available with `--help`: {ref}`CLI help for predict <cli-help-predict>`."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Video\n",
    "\n",
    "```sh\n",
    "python3 -m openpifpaf.video --source myvideotoprocess.mp4 --video-output --json-output\n",
    "```\n",
    "\n",
    "Requires OpenCV. The `--video-output` option also requires matplotlib.\n",
    "Replace `myvideotoprocess.mp4` with `0` for webcam0 or other OpenCV compatible sources.\n",
    "The full list of arguments is available with `--help`: {ref}`CLI help for video <cli-help-video>`."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Image credit: \"[Learning to surf](https://www.flickr.com/photos/fotologic/6038911779/in/photostream/)\" by fotologic which is licensed under [CC-BY-2.0].\n",
    "\n",
    "[CC-BY-2.0]: https://creativecommons.org/licenses/by/2.0/"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "python3",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.4-final"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
